package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.boltshow.R;
import com.google.gson.Gson;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.AllPraiseAdapter;
import application.MyApplication;
import config.NetConfig;
import model.AllPraiseModel;
import model.UserCollect;

/**
 * Created by lenovo on 2016/8/29.
 */
public class MineAllPraiseFragment extends Fragment {
    private RecyclerView mRecyclerView;
    private TextView mTextView;
    private RelativeLayout mRelativeLayout;
    private DbManager mDb;
    private GridLayoutManager manager;
    private AllPraiseAdapter mAdapter;
    private  List<AllPraiseModel.BrandsBean> mData;
    private List< AllPraiseModel.BrandsBean> mPraise;


    // private List<>;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.fragmenta_mineallfragment_show,null);
        initView(view);
        initData();
        setData();
        return view ;
    }

    private void setData() {
        manager = new GridLayoutManager(getContext(),2, LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);

    }

    private void initData() {
        mData=new ArrayList<>();
        mPraise=new ArrayList<>();
        mAdapter=new AllPraiseAdapter(getActivity(),mPraise);

        x.http().get(new RequestParams(NetConfig.PATH), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(result!=null) {
                    Gson gson = new Gson();
                    AllPraiseModel allPraiseModel = gson.fromJson(result, AllPraiseModel.class);
                    List<AllPraiseModel.BrandsBean> brands = allPraiseModel.getBrands();
                    mData.addAll(brands);

                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

        MyApplication myApplication= (MyApplication) getActivity().getApplication();
        String userName = myApplication.getUserName();

        try {
            List<UserCollect> name = mDb.selector(UserCollect.class).where("name", "=", userName).findAll();
            if(name!=null){

                mTextView.setVisibility(View.INVISIBLE);
                for (int i = 0; i < name.size(); i++) {
                    String brandId = name.get(i).getBrandId();
                    for (int j = 0; j <mData.size() ; j++) {
                         if(brandId.equals(mData.get(j).getBrandId())) {
                             AllPraiseModel.BrandsBean brandsBean = mData.get(i);
                             mPraise.add(brandsBean);

                         }
                        mAdapter.notifyDataSetChanged();
                    }

                }
            }else {
                mTextView.setVisibility(View.VISIBLE);
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }



    private void initView(View view) {
        DbManager.DaoConfig config = new DbManager.DaoConfig();
        mDb=x.getDb(config);
        mRecyclerView= (RecyclerView) view.findViewById(R.id.rv_minePraise_show);
        mTextView= (TextView) view.findViewById(R.id.tv_minePraise_empty);
        mRelativeLayout= (RelativeLayout) view.findViewById(R.id.layout_minePraise);

    }








}
